<style>
    .layui-form-item .layui-inline {
        margin-top: 10px;
    }
</style>
<div class="layui-fluid" id="Ship-create">
    <div class="layui-card">
        <div class="layui-form" lay-filter="LAY-list-form">
            <div id="view"></div>
        </div>
    </div>
</div>
<script id="page1" type="text/html">
    <input type="hidden" name="id" value="{{layui.router().search.id||''}}">
    <div class="layui-card-header">采购单资料</div>
    <div class="layui-card-body">
        <div class="layui-form-item">
            {{# if(layui.router().search.id>0){  }}
            <div class="layui-inline">
                <label class="layui-form-label">采购单号:</label>
                <div class="layui-input-inline">
                    <span class="layui-input layui-input-none">{{d.result.code||''}}</span>
                </div>
            </div>
            {{# } }}
            {{# if(d.result.type==1){ }}
            <div class="layui-inline">
                <label class="layui-form-label">业务单号:</label>
                <div class="layui-input-inline">
                    <span class="layui-input layui-input-none">{{d.result.bs_code||''}}</span>
                </div>
            </div>
            {{# } }}
            <div class="layui-inline">
                <label class="layui-form-label">采购公司:</label>
                <div class="layui-input-inline  layui-jy-width200">
                {{# if(layui.router().search.id>0){ }}
                <span class="layui-input layui-input-none">{{d.result.purchase_company_str||''}}</span>
                {{# }else{ }}
                            <select name="purchase_company_id" lay-verify="required" lay-search>
                                <option value="">请选择</option>
                                {{# for(var i in d.info.lsCompany){ if(i==d.result.purchase_company_id){ var
                                selected='selected';}else{
                                var selected=''; } }}
                                <option value="{{i}}" {{selected}}>{{d.info.lsCompany[i]}}</option>
                                {{# } }}
                            </select>
                {{# } }}
                </div>
            </div>

            {{# if(!layui.router().search.id||d.result.type==2){ }}
            <div class="layui-inline">
                <label class="layui-form-label">收货仓库:</label>
                <div class="layui-input-inline">
                    <select name="receipt_warehouse_id" lay-verify="required" lay-search>
                        <option value="">请选择</option>
                        {{# for(var i in d.info.countryWarehouse){ if(i==d.result.receipt_warehouse_id){ var
                        selected='selected';}else{ var selected=''; } }}
                        <option value="{{i}}" {{selected}}>{{d.info.countryWarehouse[i]}}</option>
                        {{# } }}
                    </select>
                </div>
            </div>
            {{# } }}
            <div class="layui-inline">
                <label class="layui-form-label  layui-label">预计到仓时间:</label>
                <div class="layui-input-inline">
                {{# if(layui.router().search.id>0){ }}
                <span class="layui-input layui-input-none">{{d.result.arrival_date||''}}</span>
                {{# }else{ }}
                    <input type="text" name="arrival_date" class="layui-input" id="arrival_date"
                           placeholder="yyyy-MM-dd"
                           lay-verify="required">
                    {{# } }}
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">供应商名称:</label>
                <div class="layui-input-inline  layui-jy-width200">
                    {{# if(layui.router().search.id>0){ }}
                    <span class="layui-input layui-input-none">{{d.result.supplier_company_str||''}}</span>
                    {{# }else{ }}
                    <select name="supplier_company_id" lay-verify="required" lay-search>
                        <option value="">请选择</option>
                        {{# for(var i in d.info.supplier){ if(i==d.result.supplier_company_id){ var
                        selected='selected';}else{
                        var selected=''; } }}
                        <option value="{{i}}" {{selected}}>{{d.info.supplier[i]}}</option>
                        {{# } }}
                    </select>
                    {{# } }}
                </div>
            </div>
            {{# if(d.result.ship_warehouse_id>0){ }}
            <div class="layui-inline">
                <label class="layui-form-label">发货仓库:</label>
                <div class="layui-input-inline">
                    <select name="ship_warehouse_id">
                        <option value="">请选择</option>
                        {{# for(var i in d.info.warehouse){ if(i==d.result.ship_warehouse_id){ var
                        selected='selected';}else{ var selected=''; } }}
                        <option value="{{i}}" {{selected}}>{{d.info.warehouse[i]}}</option>
                        {{# } }}
                    </select>
                </div>
            </div>
            {{# } }}
            <div class="layui-inline">
                <label class="layui-form-label layui-label">备注:</label>
                <div class="layui-input-inline">
                    {{# if(layui.router().search.id>0){ }}
                    <span class="layui-input layui-input-none">{{d.result.remark||''}}</span>
                    {{# }else{ }}
                    <input type="text" value="{{d.result.remark||''}}" name="remark" class="layui-input ">
                    {{# } }}
                </div>
            </div>
            {{# if(layui.router().search.id>0){}}
            <div class="layui-inline">
                <label class="layui-form-label">供应商地址:</label>
                <div class="layui-input-inline">
                    <span class="layui-input layui-input-none">{{d.result.company_addr||''}}</span>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">采购金额:</label>
                <div class="layui-input-inline">
                    <span class="layui-input layui-input-none">{{d.result.money||''}}</span>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">订单类型:</label>
                <div class="layui-input-inline">
                    <span class="layui-input layui-input-none">{{d.result.type_str||''}}</span>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">创建时间:</label>
                <div class="layui-input-inline">
                    <span class="layui-input layui-input-none">{{d.result.ctime||''}}</span>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">创建人员:</label>
                <div class="layui-input-inline">
                    <span class="layui-input layui-input-none">{{d.result.user||''}}</span>
                </div>
            </div>
            {{# } }}
        </div>
    </div>

    <div class="layui-card-header">商品信息</div>
    <div class="layui-card-body">

        <div class="layui-item">
            {{# if(!layui.router().search.id) { }}
            <button class="uploadPage layui-btn">导入</button>
            <button class="add layui-btn">新增</button>
            {{#} }}
            <button class="export1 layui-btn">导出</button>
        </div>
        <table id="LAY-list" lay-filter="LAY-list"></table>

    </div>

    <div class="layui-card-body layui-jy-center">
        <a lay-href="Purchasemanage/index" class="layui-btn">返回</a>
        {{# if(!layui.router().search.id) { }}
        <input type="button" lay-submit lay-filter="LAY-submit-1" value="保存" class="layui-btn LAY-submit1">
        {{#} }}
    </div>

</script>
<!-- 行内工具 -->
<script type="text/html" id="LAY-list-toolbar">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="del" href="javascript:;">删除</a>
</script>
<script>
    layui.use(['admin', 'form', 'toolJ', 'laytpl', 'laydate', 'upload', 'table'], function () {
        var $ = layui.$
            , form = layui.form
            , laytpl = layui.laytpl
            , setter = layui.setter
            , laydate = layui.laydate
            , table = layui.table
            , toolJ = layui.toolJ;
        var id = layui.router().search.id;
        //新增
        $('#Ship-create').off('click.add').on('click.add', '.add', function () {
            table.cache["LAY-list"].unshift({"sku": "", "sku_name": "", "quantity": "", "unit_price": "", "money": ""});
            tableView(table.cache["LAY-list"]);
        })
        //监听上传按钮
        $('#Ship-create').off('click.uploadPage').on('click.uploadPage', '.uploadPage', function () {
            if (typeof layui.table.cache['LAY-list'] != 'undefined' && table.cache['LAY-list'].length > 0) {
                layer.confirm('已存在数据,重新导入会将之前导入的清空是否确定!', function () {
                    upload();
                });
            } else {
                upload();
            }
        })
        //导出
        $("body").off('click.export1').on('click.export1', '.export1', function () {
            $(".layui-table-tool .layui-inline[lay-event='LAYTABLE_EXPORT']").click();
            $(".layui-table-tool li[data-type='csv']").click();
        })

        function tableView(data) {
            var url = '';
            var jdata = '';
            if (data.length > 0) {
                jdata = data;
            } else {
                url = 'index/Ship/detail';
            }
            table.render({
                elem: '#LAY-list'
                , url: url
                , data: jdata
                , toolbar: true
                , defaultToolbar: ['exports', 'filter', 'print']
                , cellMinWidth: 80
                , cols: [[
                    {field: 'sku', title: 'sku', edit: 'text'}
                    , {field: 'sku_name', title: '采购名称'}
                    , {field: 'quantity', title: '采购数量', edit: 'text'}
                    , {field: 'unit_price', title: '单价', edit: 'text'}
                    , {field: 'money', title: '金额'}
                    , {title: '操作', align: 'center', fixed: 'right', toolbar: '#LAY-list-toolbar'}
                ]]
                , page: false
                , limit: 100000
                , text: '加载出现异常！'
            });
        }

        //详情
        function tableView2(data) {
            table.render({
                elem: '#LAY-list'
                , data: data
                , toolbar: true
                , defaultToolbar: ['exports', 'filter', 'print']
                , cellMinWidth: 80
                , cols: [[
                    {field: 'sku', title: 'sku'}
                    , {field: 'sku_name', title: '采购名称'}
                    , {field: 'quantity', title: '采购数量'}
                    , {field: 'unit_price', title: '单价'}
                    , {field: 'money', title: '金额'}
                ]]
                , page: false
                , limit: 100000
                , text: '加载出现异常！'
            });
        }

        function upload() {
            //第一个链接为上传api 第二个链接是模板下载链接 第三个是成功回调
            toolJ.upload('/index/Purchasemanage/upload', '/attachment/importcvs/Purchasemanagecreate.csv', function (res) {
                if (res.code == 1) {
                    tableView(res.data);
                }
            });
        }

        function viewHtml(data) {
            var getTpl = page1.innerHTML
                , view = $('#view');
            laytpl(getTpl).render(data, function (html) {
                view.html(html);
            });
            laydate.render({
                elem: '#arrival_date'
                , value: data.result.arrival_date ? data.result.arrival_date : ''
            });
            if (data.type == 1) {
                tableView2(data.detail);
            } else {
                tableView(data.detail);
            }
            table.render(null, 'LAY-list');
            form.render(null, 'LAY-list-form');
            //监听提交
            form.on('submit(LAY-submit-1)', function (data) {
                if (typeof table.cache['LAY-list'] != 'undefined' && table.cache['LAY-list'].length > 0) {
                    data.field.list = JSON.stringify(table.cache['LAY-list']);
                    toolJ.ajax({"data": data.field});
                } else {
                    toolJ.error('不存在明细!');
                }
            })
            //监听行内操作
            table.on('tool(LAY-list)', function (obj) {
                switch (obj.event) {
                    case 'del':
                        layer.confirm('确定删除这条数据吗？', function (index) {
                            obj.del();
                            layer.close(index);
                        });
                        break;
                }
            });

        }

        var info = {};
        info.shop = layui.data(setter.tableName).shop;
        info.warehouse = layui.data(setter.tableName).warehouse;
        info.company = layui.data(setter.tableName).company;
        info.warehouseCountry = layui.data(setter.tableName).warehouseCountry;
        if (id) {
            toolJ.ajax({"data": {"id": id}, "type": "get"},
                function (data) {
                    info.myCompany=data.data.myCompany;
                    info.lsCompany=data.data.lsCompany;
                    info.countryWarehouse=data.data.countryWarehouse;
                    info.supplier=data.data.supplier;
                    viewHtml({"info": info, "result": data.data.result, "type": 1, "detail": data.data.detail});
                }
            );
        } else {
            toolJ.ajax({"data": {}, "type": "get"},
                function (data) {
                    info.myCompany=data.data.myCompany;
                    info.lsCompany=data.data.lsCompany;
                    info.supplier=data.data.supplier;
                    info.countryWarehouse=data.data.countryWarehouse;
                    viewHtml({"info": info, "result": {}, "detail": {"code": 1, "data": null}, "type": 0});
                }
            );
        }
        table.on('edit(LAY-list)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
            //编辑sku更新 更新sku相关字段
            if (obj.field == 'sku') {
                obj.data.sku = obj.value.toUpperCase();
                $.ajax({
                    type: 'post',
                    url: '/index/common/getSku',
                    dataType: 'json',
                    data: {"sku": obj.data.sku},
                    success: function (data) {
                        obj.data.sku_name = '';
                        obj.update(
                            obj.data
                        );
                        if (data.code == 1) {
                            obj.data.sku_name = data.data.cnname;
                            obj.update(
                                obj.data
                            );
                        }
                    }
                })
            } else {
                //重算金额
                obj.data.money = obj.data.quantity * obj.data.unit_price;
                obj.update(
                    obj.data
                );
            }
        });

    })
</script>